// 留言
var express = require('express');
var router = express.Router();
var db = require('../model/index');

/* GET  */
router.get('/messageList', function (req, res) {
    //   res.send('respond with a resource');
    var sql = `SELECT * FROM messagelist`
    db(sql, null, function (err, data) {
        if (!err) {
            //    console.log(123);
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 获取留言内容
router.get('/message', function (req, res) {
    //   res.send('respond with a resource');
    console.log(req.query.messageId);
    var sql = `SELECT * FROM message where messageId = ?`
    db(sql, [req.query.messageId], function (err, data) {
        if (!err) {
            //    console.log(123);
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 添加留言
router.get('/addMessage', function (req, res) {
    var sql = `INSERT into message (messageId,memberName,content,createTime) VALUES (?,?,?,?)`

    db(sql, [req.query.messageId, req.query.memberName, req.query.content, req.query.createTime], function (err, data) {

        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 留言互助举报
router.get('/jubaoMessage', function (req, res) {
    var sql = `INSERT into messagejubao (jubao,messageId,userId) VALUES (?,?,?);UPDATE messagelist SET messageAudit=2 WHERE messageId = ?`
    db(sql, [req.query.jubao, req.query.messageId, req.query.userId, req.query.messageId], function (err, data) {
        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    });

});
// 添加 addMessage
router.post('/addMessage', function (req, res) {
    console.log(req.body);
    var sql = `INSERT into messagelist (InfoPublisher,messageAudit,
                messageDesc,
                messageImg,
            messageTitle,
            messageType) VALUES (?)
            `

    let params = [req.body.InfoPublisher, 1,
    req.body.messageDesc,
    req.body.messageImg,
    req.body.messageTitle,
        "留言板"]


    db(sql, [params], function (err, data) {
        if (!err) {
            //    console.log(123);
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    });

});
// 寻物图片批量
router.post('/addBarterImgs', function (req, res) {
    let arr = req.body.barterImgs; // 前端提交过来的数据
    console.log(arr, 11);
    let sql = `INSERT INTO barterimg (barterId,barterImg) VALUES(?,?)`    // 插入语句
    for (let i = 0; i < arr.length; i++) {
        db(sql, [req.body.barterId, arr[i]], function (err, data) {
            if (err) {
                console.log(err)
                if (i == arr.length - 1) {
                    res.send({
                        code: 500,
                        msg: "添加失败!",
                    });
                }
            } else {
                console.log(data);
                if (i == arr.length - 1) {
                    res.send({
                        msg: "添加成功!",
                        code: 200
                    });
                }
            }
        });
    }
});
// 寻物搜索
router.get('/searchTitle', function (req, res) {
    console.log(req.query.searchTitle);
    var sql = `select * from barterlist where barterTitle  REGEXP ?`
    db(sql, [req.query.searchTitle], function (err, data) {
        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail",
            })
        }
    });

});
module.exports = router;